<template>
  <el-select
    v-model="model"
    :multiple="multiple"
    placeholder="请选择"
    @change="change"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    />
    <template #empty>
      <div v-if="baseStore.loadError" class="my-2">
        <div class="mb-2">数据加载失败</div>
        <el-button
          type="primary"
          size="small"
          @click="baseStore.getCommunityList"
          >重新加载</el-button
        >
      </div>
      <div v-else>
        <div class="my-2">暂无数据，请先添加小区</div>
        <el-button
          type="primary"
          size="small"
          @click="baseStore.getCommunityList"
          >重新加载</el-button
        >
      </div>
    </template>
  </el-select>
</template>

<script setup>
import { toRaw } from "vue";
import { cloneDeep } from "lodash";
import useBaseStore from "@/store/modules/base";

const baseStore = useBaseStore();
const props = defineProps({
  multiple: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits(["change"]);
const model = defineModel();

const options = computed(() => {
  return baseStore.communityList;
});

const change = (value) => {
  const findItem = toRaw(options.value).find((item) => item.id === value);
  emit("change", value, cloneDeep(findItem));
};
</script>

<style scoped>
.el-select {
  min-width: 178px;
  width: 100%;
}
</style>
